import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class TabBottomBar extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new ScaffoldRoute();
  }
}

class ScaffoldRoute extends StatefulWidget {
  @override
  _ScaffoldRouteState createState() => _ScaffoldRouteState();
}

class _ScaffoldRouteState extends State<ScaffoldRoute>
  with SingleTickerProviderStateMixin {
  //定义一个私有的变量
  int _selectIndex = 1;

  //定义一个Controller 控制器
  TabController _tabController;

  //定义集合数据
  List tabs = ["新闻", "历史", "图片"];

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: tabs.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    _tabController.addListener(() {
      switch (_tabController.index) {
      }
    });
    return Scaffold(
      appBar: AppBar(
        title: Text("App Name"),
        leading: Builder(builder: (context) {
          return IconButton(
            icon: Icon(Icons.dashboard),
            color: Colors.white,
            onPressed: () {
              Scaffold.of(context).openDrawer();
            });
        }),
        actions: <Widget>[
          IconButton(icon: Icon(Icons.share), onPressed: () {})
        ],
        bottom: TabBar(
          controller: _tabController,
          tabs: tabs.map((e) => Tab(text: e)).toList()),
      ),
      drawer: new MyDrawer(),
      body: TabBarView(
      controller: _tabController,
      children: tabs.map((e) {
        return Container(
          alignment: Alignment.center,
          child: Text(e, textScaleFactor: 5),
        );
      }).toList()),
      bottomNavigationBar: BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home), title: Text('Home')),
          BottomNavigationBarItem(
            icon: Icon(Icons.business), title: Text('Business')),
          BottomNavigationBarItem(
            icon: Icon(Icons.school), title: Text('School')),
        ],
        currentIndex: _selectIndex,
        fixedColor: Colors.blue,
        onTap: _onItemTapped,
      ),
    );
  }

  void _onItemTapped(int index) {
    Fluttertoast.showToast(
      msg: "弹出的Toast$index",
      toastLength: Toast.LENGTH_LONG,
      gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
      backgroundColor: Colors.blue,
      textColor: Colors.red);
    setState(() {
      _selectIndex = index;
    });
  }
}

class HomeWidgetDemo extends StatefulWidget {
  @override
  _HomeWidgetDemo createState() => new _HomeWidgetDemo();
}


class _HomeWidgetDemo extends State<HomeWidgetDemo> {
  @override
  Widget build(BuildContext context) {

    return Center(
      child: Text("首页"),
    );
  }
}


/**
 * 抽屉菜单Drawer
 */
class MyDrawer extends StatelessWidget {
  const MyDrawer({
    Key key,
  }) : super(key: key);

  /*
   *  MediaQuery.removePadding : 移除 元素的内边距
   */

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: MediaQuery.removePadding(
        context: context,
        removeTop: true, //移除上方的内边距
        child: Column(
          //设置纵轴方向的对齐方式
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(top: 38.0),
              child: Row(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 16.0),
                    child: ClipOval(
                      child: Image.asset(
                        "assets/images/image4.jpg",
                        width: 80,
                      ),
                    ),
                  ),
                  Text("Wendux",
                    style: TextStyle(fontWeight: FontWeight.bold))
                ],
              ),
            ),
            Expanded(
              child: ListView(
                children: <Widget>[
                  ListTile(
                    leading: const Icon(Icons.add),
                    title: const Text('Add account'),
                  ),
                  ListTile(
                    leading: const Icon(Icons.settings),
                    title: const Text('Manage accounts'),
                  ),
                ],
              ))
          ],
        )),
    );
  }
}
